package com.spring.campus_express_delivery.controller;

import com.spring.campus_express_delivery.entity.PageBean;
import com.spring.campus_express_delivery.entity.SchoolAddress;
import com.spring.campus_express_delivery.entity.Users;
import com.spring.campus_express_delivery.interceptor.RequiredPermission;
import com.spring.campus_express_delivery.service.SchoolAddressService;
import com.spring.campus_express_delivery.util.JwtToken;
import com.spring.campus_express_delivery.util.MyUtil;
import com.spring.campus_express_delivery.util.RedisUtil;
import com.spring.campus_express_delivery.util.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@CrossOrigin
@Controller
@RequestMapping("/school_address")
@Api(value = "SchoolAddressController", description = "送达地址列表相关api")
public class SchoolAddressController {
    Boolean flag;
    MyUtil myUtil = new MyUtil();
    @Autowired
    SchoolAddressService schoolAddressService;
    @Autowired
    RedisUtil redisUtil;
    /**
     * 送达地址列表
     * @param size
     * @param page
     * @param sort
     * @param asc
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("送达地址列表")
    @PostMapping("/wx/serviceAddress_list")
    @ResponseBody
    public ResponseEntity selectServiceAddressByPage(@RequestParam(value = "size",required = false,defaultValue = "10") int size,
                                      @RequestParam(value = "page",required = false,defaultValue = "1") int page,
                                      @RequestParam(value = "sort",required = false,defaultValue ="id") String sort,
                                      @RequestParam(value = "asc",required = false,defaultValue = "asc") String asc,
                                                     @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        PageBean<SchoolAddress> pagemsg = schoolAddressService.selectServiceAddressByPage(size, page, sort, asc);
        responseEntity = ResponseEntity.ok();
        responseEntity.putDataValue("records",pagemsg);
        return responseEntity;
    }
    /**
     * 添加送达地址类型
     *
     * @param serviceAddress
     * @param Authorization
     * @throws IOException
     * @rueturn
     */
    @ApiOperation("添加送达地址类型")
    @PostMapping("/web/insertServiceAddress")
    @ResponseBody
    @RequiredPermission(1)
    public ResponseEntity insertServiceAddress(@RequestParam(required = true) String serviceAddress, @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        Users users = JwtToken.unsign(token, Users.class);
        SchoolAddress schoolAddress1 = schoolAddressService.selectByServiceAddress(serviceAddress);
        if (schoolAddress1 == null) {
            responseEntity = ResponseEntity.ok();
            SchoolAddress schoolAddress = new SchoolAddress();
            schoolAddress.setServiceAddress(serviceAddress);
            schoolAddressService.insertServiceAddress(schoolAddress);
            responseEntity.putDataValue("msg", "添加成功");
            return responseEntity;
        } else {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "送达地址已存在无法添加");
            return responseEntity;
        }


    }

    /**
     * 修改快递送达地址类型
     *
     * @param id
     * @param serviceAddress
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("修改快递送达地址类型")
    @PutMapping("/web/updateShoolAddress")
    @ResponseBody
    @RequiredPermission(1)
    public ResponseEntity updateShoolAddress(@RequestParam(required = true) int id, @RequestParam(required = false) String serviceAddress, @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        Users users = JwtToken.unsign(token, Users.class);
        responseEntity = ResponseEntity.ok();
        SchoolAddress schoolAddress = schoolAddressService.selectByServiceAddressId(id);
        if (schoolAddress.getServiceAddress() != serviceAddress) {
            schoolAddress.setServiceAddress(serviceAddress);
            schoolAddressService.updateSchoolAddress(schoolAddress);
            responseEntity.putDataValue("msg", "修改成功");
            //删除缓存
            String key = "schoolAddress_" + id;
            if (redisUtil.hasKey(key)) {
                redisUtil.del(key);
            }
            return responseEntity;
        } else {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "送达地址已存在，修改失败");
            return responseEntity;
        }

    }

    /**
     * 根据id查询快递送达地址
     * @param id
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据id查询快递送达地址类型")
    @GetMapping("/web/selectBySchoolAddressId")
    @ResponseBody
    public ResponseEntity selectBySchoolAddressId(@RequestParam(required = true) Integer id, @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        Users users = JwtToken.unsign(token, Users.class);
        String key = "schoolAddress_" + id;
        boolean b = redisUtil.hasKey(key);
        //如果redis中存在 查询缓存
        if (b){
            SchoolAddress schoolAddress = (SchoolAddress) redisUtil.get(key);
            responseEntity=ResponseEntity.ok();
            responseEntity.putDataValue("schoolAddress",schoolAddress);
            return responseEntity;
        }
        responseEntity=ResponseEntity.ok();
        SchoolAddress schoolAddress = schoolAddressService.selectByServiceAddressId(id);
        responseEntity.putDataValue("schoolAddress",schoolAddress);
        //将schoolAddress缓存到redis
        redisUtil.set("schoolAddress_" + id, schoolAddress);
        return responseEntity;
    }


    /**
     * 根据id删除快递送达地址
     * @param id
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据id删除快递送达地址类型")
    @DeleteMapping("/web/deleteSchoolAddressById")
    @ResponseBody
    @RequiredPermission(1)
    public ResponseEntity deleteSchoolAddressById(@RequestParam(required = true) Integer id,@RequestHeader String Authorization)throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        Users users = JwtToken.unsign(token, Users.class);
        SchoolAddress schoolAddress = schoolAddressService.selectByServiceAddressId(id);
        if (schoolAddress != null) {
            schoolAddressService.deleteSchoolAddressById(id);
            responseEntity = ResponseEntity.ok();
            responseEntity.putDataValue("msg", "删除成功");
            //删除缓存
            String key = "schoolAddress_" + id;
            if (redisUtil.hasKey(key)) {
                redisUtil.del(key);
            }
            return responseEntity;
        }else{
            responseEntity=ResponseEntity.forbidden();
            responseEntity.putDataValue("msg","快递送达地址类型不存在，删除失败");
            return responseEntity;
        }
    }
}
